package com.dnjn.mes.biz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dnjn.mes.biz.domain.MesEquipment;
import com.dnjn.mes.biz.domain.RunStatus;
import com.dnjn.mes.biz.enums.BoolType;
import com.dnjn.mes.biz.mapper.MesEquipmentMapper;
import com.dnjn.mes.biz.service.IMesEquipmentService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;

/**
 * <p>
 * 设备 服务实现类
 * </p>
 *
 * @author Amber
 * @since 2023-07-26
 */
@Service("iMesEquipmentService")
public class MesEquipmentServiceImpl extends ServiceImpl<MesEquipmentMapper, MesEquipment> implements IMesEquipmentService {

    @Override
    public MesEquipment selectByEquipmentName(String equipmentName) {
        return lambdaQuery()
                .eq(MesEquipment::getEquipmentName, equipmentName)
                .one();
    }

    @Override
    public MesEquipment fetchCurrent() {
        return lambdaQuery()
                .eq(MesEquipment::getDeleted, "0")
                .eq(MesEquipment::getIsDefault, BoolType.YES)
                .one();
    }

    @Override
    public ArrayList<RunStatus> countStatusTime(Long equipmentId) {
        MesEquipment equipment = lambdaQuery().eq(MesEquipment::getEquipmentId, equipmentId).one();
        BigDecimal durationRunning = equipment.getDurationRunning();//运行时长
        BigDecimal durationStop = equipment.getDurationStop();//停机时长
        BigDecimal durationAwait = equipment.getDurationAwait();//待机时长
        BigDecimal durationMaintain = equipment.getDurationMaintain();//维修时长
        BigDecimal sumTime = durationRunning.add(durationStop);
        BigDecimal running = new BigDecimal(0);
        BigDecimal stop = new BigDecimal(0);
        BigDecimal await = new BigDecimal(0);
        BigDecimal maintain = new BigDecimal(0);
        if (!durationRunning.equals(BigDecimal.ZERO) && durationRunning != null && !"0.00".equals(durationRunning.toString())) {
            running = durationRunning.divide(sumTime, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));

        }
        if (!durationStop.equals(BigDecimal.ZERO) && durationStop != null && !"0.00".equals(durationStop.toString())) {
            stop = durationStop.divide(sumTime, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));

        }
        if (!durationAwait.equals(BigDecimal.ZERO) && durationAwait != null && !"0.00".equals(durationAwait.toString())) {
            await = durationAwait.divide(sumTime, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));

        }
        if (!durationMaintain.equals(BigDecimal.ZERO) && durationMaintain != null && !"0.00".equals(durationMaintain.toString())) {
            maintain = durationMaintain.divide(sumTime, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));

        }
        ArrayList<RunStatus> list = new ArrayList<RunStatus>();
        RunStatus status1 = new RunStatus();
        status1.setDeviceName("运行");
        status1.setRunTime(equipment.getDurationRunning().intValue());
        status1.setPtg(running.intValue());
        RunStatus status2 = new RunStatus();
        status2.setDeviceName("停止");
        status2.setRunTime(equipment.getDurationStop().intValue());
        status2.setPtg(stop.intValue());
        RunStatus status3 = new RunStatus();
        status3.setDeviceName("待机");
        status3.setRunTime(equipment.getDurationAwait().intValue());
        status3.setPtg(await.intValue());
        RunStatus status4 = new RunStatus();
        status4.setDeviceName("维修");
        status4.setRunTime(equipment.getDurationMaintain().intValue());
        status4.setPtg(maintain.intValue());
        list.add(status1);
        list.add(status2);
        list.add(status3);
        list.add(status4);
        return list;
    }
}
